package com.yanqu.road.server.logic.activity.condition.recharge.potential;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.log.LogTriggerRMBMallBuy;
import com.yanqu.road.entity.log.LogTriggerRMBMallTrigger;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PotentialModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.MallRechargeConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;

import java.math.BigInteger;
import java.util.List;

public abstract class BaseRechargePotentialCondition  extends BaseActivityCondition {

    public BaseRechargePotentialCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        PotentialArg arg = (PotentialArg) object;

        int mallId = arg.getMallId();
        if (mallId > 0) {
            // 到账了
            if (mallId == getValue().intValue()) {
                rechargeSuccess(mallId);
            }
            return;
        }

        if (getValue().intValue() > 0) {
            //已经触发过了
            return;
        }

        //是否触发过，已经触发不用再出发
        if (arg.isLogin() && getPreValue().equals(BigInteger.ONE)) {
            //触发
            setMallInfo();
            return;
        }

        if (getPreValue().equals(BigInteger.ONE)) {
            //已经触发过了
            return;
        }



        if (!checkPotential()) {
            return;
        }

        if (checkCondition()) {
            //都满足了
            //使用 preValue 来设置是否下次登录触发
            if (isNextLoginActive()) {
                setPreValue(BigInteger.ONE);
                return;
            }
            setMallInfo();
        }
    }

    protected abstract boolean checkCondition();


    protected boolean checkTitle() {
        List<BigInteger> paramList = activityConditionInfo.getParamList();
        int needTitleId = paramList.get(0).intValue();
        if (needTitleId == -1) {
            return true;
        }
        int titleId = player.getUserInfo().getTitleId();
        if (needTitleId == 0) {
            return needTitleId == titleId;
        }
        return titleId >= needTitleId;
    }

    protected boolean checkRechargeTimes() {
        List<BigInteger> paramList = activityConditionInfo.getParamList();
        int needRechargeTimes = paramList.get(1).intValue();
        if (needRechargeTimes == -1) {
            return true;
        }
        long totalRechargeTimes = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.RechargeTotalTimes);
        if (needRechargeTimes == 0) {
            return needRechargeTimes == totalRechargeTimes;
        }
        return totalRechargeTimes >= needRechargeTimes;
    }

    protected boolean checkRechargeAmount() {
        List<BigInteger> paramList = activityConditionInfo.getParamList();
        int needRechargeAmount = paramList.get(2).intValue();
        if (needRechargeAmount == -1) {
            return true;
        }
        long totalRecharge = player.getUserInfo().getVipExp();
        if (needRechargeAmount == 0) {
            return needRechargeAmount == totalRecharge;
        }
        return totalRecharge >= needRechargeAmount;
    }

    protected boolean checkPotential() {
        return true;
    }

    protected boolean isNextLoginActive() {
        List<BigInteger> paramList = activityConditionInfo.getParamList();
        int nextLoginActive = paramList.get(4).intValue();
        return nextLoginActive == 1;
    }

    protected void setMallInfo() {
        String extendParam = activityConditionInfo.getExtendParam();
        String[] arr = extendParam.split("\\|");
        if (arr.length <= 0) {
            return;
        }
        int potential = player.getModule(PotentialModule.class).getPotential();
        if (potential <= 0) {
            return;
        }
        int index = 0;
        if (arr.length >= potential) {
            index = potential - 1;
        }else {
            index = arr.length - 1;
        }
        String s = arr[index];
        String[] mallStr = s.split(";");
        int mallId = Integer.parseInt(mallStr[0]);
        if (mallId <= 0) {
            return;
        }
        MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(activityInfo.getActivityId(), mallId);
        if (mallInfo == null) {
            return;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        setParam(String.valueOf(nowTime + NormalActivityMgr.getRMBMallOverTime(activityInfo)));      //设置结束时间
        setValue(BigInteger.valueOf(mallInfo.getMallId()));
        //增加触发日志
        long price = MallRechargeConfigMgr.getPrice(mallInfo);
        AutoLogMgr.add(new LogTriggerRMBMallTrigger(player.getUserId(), activityInfo.getActivityId(), activityConditionInfo.getConditionId(),
                activityConditionInfo.getType(), player.getTitleId(), mallInfo.getMallId(), nowTime, price));
    }


    private void rechargeSuccess(int mallId) {
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(getConditionId());
        if(!userActivityConditionData.isGetReward()) {
            userActivityConditionData.setGetReward(true);
            MallInfo mallInfo = MallMgr.getMallInfoByMallId(mallId);
            String reward = "";
            if(null == mallInfo){
                logger.error("RMBMallActivity mall no find,mallId:{}", mallId);
                return;
            }else {
                reward = mallInfo.getReward();
                player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.RMBMallActivityReward);
            }
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            player.getModule(ActivityModule.class).onActivityDataChanged(baseActivityData);
            LogMgr.addLogActivityReward(player.getUserId(), activityInfo.getActivityId(), activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), 0, false, reward);
            long price = MallRechargeConfigMgr.getPrice(mallInfo);
            AutoLogMgr.add(new LogTriggerRMBMallBuy(player.getUserId(), activityInfo.getActivityId(), activityConditionInfo.getConditionId(),
                    activityConditionInfo.getType(), mallId, price));
        }else {
            logger.error("already get reward activityId:{}; conditionId:{}", activityInfo.getActivityId(), getConditionId());
        }
    }
}
